<template>
  <view v-if="goods_info.goods_name" class="goods_info_con">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :circular="true" :duration="1000">
      <swiper-item v-for="(item,inx) in goods_info.pics" :key="inx">
        <image :src="item.pics_big" @click="preview(inx)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品详情区域 -->
    <view class="goods_info_box">
      <view class="goods_info_price">
        ${{goods_info.goods_price}}
      </view>
      <view class="goods_info">
        <text>{{goods_info.goods_name}}</text>
        <view class="star">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="kuai">
        快递:免运费
      </view>
    </view>
    <!-- 富文本图文信息 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
      @buttonClick="buttonClick" />
  </view>
</template>

<script>
  import {
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        goods_info: {}, // 商品详情数据
        goods_id: '',
        // 商品导航组件配置信息
        options: [{
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }
      ],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      }
    },
    computed:{
      ...mapGetters('m_cart',['total'])
    },
    watch:{
      // 监听购物车数目变化
      total: {
        handler(newVal){
          const tarobj = this.options.find(item => item.text === '购物车')
          tarobj.info = newVal
        },
         // immediate 属性用来声明此侦听器，是否在页面初次加载完毕后立即调用
        immediate: true
      }
    },
    onLoad(option) {
      console.log(this.total)
      this.goods_id = option.id
      this.getGoodsDetailList(this.goods_id)
    },
    methods: {
      // 获取商品详情数据
      async getGoodsDetailList(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
        // 字符串的全局替换
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
          .replace(/webp/g, 'jpg')
        this.goods_info = res.message
      },
      // 点击轮播图片进行预览
      preview(inx) {
        uni.previewImage({
          current: inx,
          urls: this.goods_info.pics.map(item => item.pics_big)
        })
      },
      // 点击左侧按钮
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/shopcar/shopcar'
          })
        }
      },
      ...mapMutations('m_cart', ['addToCart']),
        // 点击右侧加入购物车按钮
        // { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
        buttonClick(e) {
          if (e.content.text === '加入购物车') {
            // 2. 组织一个商品的信息对
            const goods = {
              goods_id: this.goods_info.goods_id, // 商品的Id
              goods_name: this.goods_info.goods_name, // 商品的名称
              goods_price: this.goods_info.goods_price, // 商品的价格
              goods_count: 1, // 商品的数量
              goods_small_logo: this.goods_info.goods_small_logo, // 商品的图片
              goods_state: true // 商品的勾选状态
            }
             // 3. 通过 this 调用映射过来的 addToCart 方法，把商品信息对象存储到购物车中
             this.addToCart(goods)
             console.log(this.total)
          }
        }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods_info_con {
    padding-bottom: 50px;

    .uni-goods-nav {
      position: fixed;
      bottom: 0;
      width: 100%;
    }
  }

  .goods_info_box {
    padding: 0 0 0 10px;

    .goods_info_price {
      color: #C00000;
    }

    .goods_info {
      display: flex;
      align-items: center;

      text {
        font-size: 12px;
      }

      .star {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 100px;
        color: gray;
        font-size: 15px;
        border-left: 1px solid #CCCCCC;
        margin-left: 20px;
      }
    }

    .kuai {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }
</style>
